<?php

namespace Admin\Model;

use Think\Model;

/**
 * 管理员模型类类
 *
 * 后台管理员增删改查封装
 *
 * @author: xuhui <xuh86@126.com>
 * @version: $Id$
 * @package: asoc.Admin.Model
 *
 */
class AdminModel extends Model {
	
	protected $tableName = "admin_user";
	
	/**
	 * 根据查询条件返回管理员分页数据
	 *
	 * @param mixed $map: 查询过滤条件
	 * @param int $currentPage:当前页
	 * @param int $pageSize: 每页大小
	 * @param string $sortBy: 排序条件
	 * @return array
	 */
	public function getItemListBy($map,$currentPage,$pageSize){
		

		$items	=	NULL;
		$count	=	0;			//总条数
		
		
		$sortBy	=	empty($sortBy) ? $this->getPk()." ASC " : $sortBy;
		
		if($map['role_id']>0){
			
			$uids		=	NULL;
			$userList	=	D('Role')->getRoleUserList($map['role_id']);
			
			if($userList){
				foreach ($userList as $key=>$val){
					$uids[]	=	$val['user_id'];
				}
			}
			$conditon['user_id']	=	array('IN',$uids);
		}
		if($map['_string']!="")	$conditon['_string']	=	$map['_string'];

			
		$items	=	$this->where($conditon)->order($sortBy)->page($currentPage.','.$pageSize)->select();
		
		if($items){
			
			$access		=	D('Access');
			foreach ($items as $key=>$val){
				
				$items[$key]['roleList']	=	$access->getAllRole($val['user_id']);
			}
		}
		
		$count	=	$this->where($conditon)->count();
		
		return array('items'=>$items,'count'=>$count);
	}
	
	
	
	/**
	 * 根据用户名获取管理员信息
	 * @param string $username: 用户名
	 * @return array
	 */
	
	public function getAdminByUsername($username){
		
		$admin		=	NULL;
		
		if($username !="" ){
			
			$admin	=	$this->where("`username`='".$username."'")->find();
		}
		
		return $admin;
	}
	
	
	/**
	 * 根据用户id获取管理员信息
	 * @param int $uid: 用户id
	 * @return array
	 */
	
	public function getAdminByUid($uid){
	
		$admin		=	NULL;
	
		if($uid >0 ){
				
			$admin	=	$this->find($uid);
		}
	
		return $admin;
	}
	
	
	/**
	 * 获取全部的管理员
	 * @param string $hasSupper:是否包含超级管理员
	 * @return array
	 */
	public function getAdminList($hasSupper=false){
		
		$admin	=	$hasSupper ? $this->select() : $this->where("user_id != 1")->select();
		
		return $admin;
	}
	
	
	/**
	 * 保存编辑和新增
	 * @param array $data:保存数组
	 * @return array
	 */
	public function actionSave($data){
		
		$statas	=	0;
		$msg	=	'操作出错';
		
		$pk		=	$this->getPk();
		$vo['email']	=	$data['email'];
		if(isset($data[$pk]) && $data[$pk]>0){
			
			if($data['password']!=""){
				
				$vo['password']	=	md5(trim($data['password']));
			}
			$this->where($pk."=".$data[$pk])->save($vo);
			
			$statas		=	1;
			$msg		=	'';
			
			//操作日志
			$loginfo	=	'编辑管理员信息，ID：'.$data[$pk];
			$this->addAdminLog($loginfo,session(C('BE_UID')));
			
		}else{
			
			$vo['add_time']	=	time();
			$vo['username']	=	$data['username'];
			$vo['password']	=	md5(trim($data['password']));
			
			$newId	=	$this->add($vo);
			
			if($newId){
				
				$statas		=	1;
				$msg		=	'';
				
				//操作日志
				$loginfo	=	'新增管理员信息，ID：'.$newId;
				$this->addAdminLog($loginfo,session(C('BE_UID')));
			}
		}
		
		return array('status'=>$statas,'msg'=>$msg);
	}
	
	
	
	/**
	 * 按条件删除
	 * @param mixed $condition: int/array 删除管理员的id   
	 * @return int:成功删除记录数
	 */
	public function deleteBy($condition){

		$delNum		=	0;
		$adminId	=	session(C('BE_UID'));
		
		//删除单个
		if(is_numeric($condition)){
				
			if($this->find($condition)){
					
				$delNum++;
				$this->delete($condition);
					
				//保存操作记录
				$this->addAdminLog('删除管理员,id:'.$condition,$adminId);
			}
		}
		//删除多个
		if(is_array($condition)){
			
			$num	=	count($condition);
			
			for($i=0;$i<$num;$i++){
				
				$recordId	=	$condition[$i];
				if($recordId>0){
					
					if($this->find($recordId)){
						
						$delNum++;
						$this->delete($recordId);
						
						//保存操作记录
						$this->addAdminLog('删除管理员,id:'.$recordId,$adminId);
					}
					
				}
			}
			
		}
		
		return $delNum;
	}
	
	
	/**
	 * 添加管理员日志
	 * @param string $loginfo:日志具体类型
	 * @param int $uid: 管理员id
	 * @param string $type: 日志类型 可选
	 * @param string $ip: 登录ip
	 */
	public function addAdminLog($loginfo,$uid,$type='action',$ip=''){
		
		//保存最近一次登陆信息
		if($type == 'login'){
			
			$this->user_id	=	$uid;
			$this->last_login_ip	=	$ip;
			$this->last_login_time	=	date('Y-m-d H:i:s');
			
			$this->save();
		}
		
		//添加日志
		$data['user_id']	=	$uid;
		$data['add_time']	=	time();
		$data['log_info']	=	$loginfo;
		$data['log_type']	=	$type;
		
		M('AdminLog')->add($data);
		
		
	}
	
	
	
}

?>